package com.outdoor.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.format.FormatterRegistry;
import org.springframework.core.convert.converter.Converter;
import com.outdoor.model.enums.TripStatus;
import com.outdoor.model.enums.ActivityType;
import com.outdoor.model.enums.ApplicationStatus;

/**
 * Web配置类
 * 
 * @author OutdoorTeam
 * @date 2025-05-18
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * 配置跨域请求
     * 
     * @param registry CORS注册表
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .maxAge(3600);
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        // 添加不区分大小写的TripStatus枚举转换器
        registry.addConverter(new StringToTripStatusConverter());
        
        // 添加不区分大小写的ActivityType枚举转换器
        registry.addConverter(new StringToActivityTypeConverter());
        
        // 添加不区分大小写的ApplicationStatus枚举转换器
        registry.addConverter(new StringToApplicationStatusConverter());
    }
    
    /**
     * 不区分大小写的TripStatus枚举转换器
     */
    private static class StringToTripStatusConverter implements Converter<String, TripStatus> {
        @Override
        public TripStatus convert(String source) {
            if (source == null || source.isEmpty()) {
                return null;
            }
            
            try {
                // 尝试直接转换
                return TripStatus.valueOf(source);
            } catch (IllegalArgumentException e) {
                // 尝试转换为大写后再转换
                try {
                    return TripStatus.valueOf(source.toUpperCase());
                } catch (IllegalArgumentException ex) {
                    throw new IllegalArgumentException("无效的行程状态: " + source + 
                        "，有效值为: " + String.join(", ", getEnumValues(TripStatus.class)));
                }
            }
        }
    }
    
    /**
     * 不区分大小写的ActivityType枚举转换器
     */
    private static class StringToActivityTypeConverter implements Converter<String, ActivityType> {
        @Override
        public ActivityType convert(String source) {
            if (source == null || source.isEmpty()) {
                return null;
            }
            
            try {
                // 尝试直接转换
                return ActivityType.valueOf(source);
            } catch (IllegalArgumentException e) {
                // 尝试转换为大写后再转换
                try {
                    return ActivityType.valueOf(source.toUpperCase());
                } catch (IllegalArgumentException ex) {
                    throw new IllegalArgumentException("无效的活动类型: " + source + 
                        "，有效值为: " + String.join(", ", getEnumValues(ActivityType.class)));
                }
            }
        }
    }
    
    /**
     * 不区分大小写的ApplicationStatus枚举转换器
     */
    private static class StringToApplicationStatusConverter implements Converter<String, ApplicationStatus> {
        @Override
        public ApplicationStatus convert(String source) {
            if (source == null || source.isEmpty()) {
                return null;
            }
            
            try {
                // 尝试直接转换
                return ApplicationStatus.valueOf(source);
            } catch (IllegalArgumentException e) {
                // 尝试转换为大写后再转换
                try {
                    return ApplicationStatus.valueOf(source.toUpperCase());
                } catch (IllegalArgumentException ex) {
                    throw new IllegalArgumentException("无效的申请状态: " + source + 
                        "，有效值为: " + String.join(", ", getEnumValues(ApplicationStatus.class)));
                }
            }
        }
    }
    
    /**
     * 获取枚举的所有可能值
     */
    private static <T extends Enum<T>> String[] getEnumValues(Class<T> enumClass) {
        T[] constants = enumClass.getEnumConstants();
        String[] values = new String[constants.length];
        for (int i = 0; i < constants.length; i++) {
            values[i] = constants[i].name();
        }
        return values;
    }
}